package org.akomantoso.core.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.akomantoso.core.data.attribute.IdReq;
import org.akomantoso.core.io.Externalizable;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.InputConstants;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.OutputUtil;

/**
 * The element timeInterval contains all the data needed to identify a specific time interval. It is built either with
 * two dates or with a date and a duration (exactly two of the 'start', 'end' and 'duration' attributes can be
 * specified). Values are of the 'start' and 'end' attributes are NOT dates, but references to event elements in the
 * corresponding metadata section. The refers attribute is a reference to a temporal concept belonging to the Akoma
 * Ntoso ontology and specified in the references section.
 * 
 * <pre>
 * <xsd:element name="temporalGroup">
 * 		<xsd:complexType>
 * 			<xsd:sequence>
 * 				<xsd:element ref="timeInterval" minOccurs="1" maxOccurs="unbounded"/>
 *			</xsd:sequence>
 * 			<xsd:attributeGroup ref="idreq"/>
 * 		</xsd:complexType>
 * </xsd:element>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 */
public final class TemporalGroup implements IdReq , Externalizable {

	public static final String ELEMENT = "temporalGroup";
	
	private String _id;
	private String _evolvingId;
	
	// Mandatory (min 1).
	private final List<TimeInterval> _timeIntervals = new ArrayList<TimeInterval>(4);

	/**
	 * @see org.akomantoso.core.data.attribute.IdReq#getId()
	 */
	public String getId() {
		return _id;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.IdReq#setId(java.lang.String)
	 */
	public void setId(String id) {
		_id = id;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.IdReq#getEvolvingId()
	 */
	public String getEvolvingId() {
		return _evolvingId;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.IdReq#setEvolvingId(java.lang.String)
	 */
	public void setEvolvingId(String evolvingId) {
		_evolvingId = evolvingId;		
	}

	public List<TimeInterval> getTimeInterval() {
		return _timeIntervals;
	}

	
	public void addTimeInterval(TimeInterval timeInterval) {
		_timeIntervals.add(timeInterval);
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#writeExternal(org.akomantoso.core.io.Output)
	 */
	public <T extends Output> void writeExternal(T out) throws IOException {
		out.writeStart(ELEMENT);
		OutputUtil.writeIdReq(out, this);
		for (int i = 0, n = _timeIntervals.size(); i < n; i++) {
			_timeIntervals.get(i).writeExternal(out);
		}
		out.writeEnd();
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#readExternal(org.akomantoso.core.io.Input)
	 */
	public <T extends Input> void readExternal(T in) throws IOException {
		
		_id = in.readAttribute(IdReq.ATTRIBUTE_ID);
		_evolvingId = in.readAttribute(IdReq.ATTRIBUTE_EVOLVING_ID);
		
		TimeInterval timeInterval;
		int eventType = in.readNext();
		while (true) {
			switch (eventType) {
			case InputConstants.START_ELEMENT:
				if (TimeInterval.ELEMENT.equals(in.getLocalName())) {
					_timeIntervals.add(timeInterval = new TimeInterval());
					timeInterval.readExternal(in);
				}
				break;
			case InputConstants.END_ELEMENT:
				if (ELEMENT.equals(in.getLocalName())) {
					return;
				}
			}
			eventType = in.readNext();
		}
		
	}
	
}